//
//  CountriesListViewController.m
//  Voxper
//
//  Created by Erphan on 2/10/11.
//  Copyright 2011 Smonte. All rights reserved.
//

#import "CountriesListViewController.h"
#import "SectionItem.h"
#import "UIManager.h"
#import "Settings.h"
#import "Country.h"
#import "VoxperAppDelegate.h"

@implementation CountriesListViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        app = (VoxperAppDelegate *)[UIApplication sharedApplication].delegate;
	}
    return self;
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark UITableView delegates
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[app.uiManager countriesList] count];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	SectionItem *object;
	object = [[app.uiManager countriesList] objectAtIndex:section];
	
	if (object) {
		if (object.sectionItems) {
			return [object.sectionItems count];
		}
	}
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	SectionItem *object;// = [app.uiManager.contacts objectAtIndex:section];

	object = [[app.uiManager countriesList] objectAtIndex:section];
	
	if (object) {
		if (object.sectionItems) {
			return object.sectionName;
		}
	}
    return @"";
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
	NSMutableArray *tempArray = [[[NSMutableArray alloc] init] autorelease];
	
	for (int i = 0;i < [[app.uiManager countriesList] count];i++) {
		SectionItem *object = [[app.uiManager countriesList] objectAtIndex:i];
		[tempArray addObject:object.sectionName];
	}

	return tempArray;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	[app.uiManager.settings setMyLocation:((Country *)[((SectionItem *)[[app.uiManager countriesList] objectAtIndex:indexPath.section]).sectionItems objectAtIndex:indexPath.row]).countryName];
	[app.uiManager.settings setMyLocationCode:((Country *)[((SectionItem *)[[app.uiManager countriesList] objectAtIndex:indexPath.section]).sectionItems objectAtIndex:indexPath.row]).dialingCode];
	[tableView reloadData];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	SectionItem *object;
	
	object = [[app.uiManager countriesList] objectAtIndex:indexPath.section];
	
	if (object) {
		if (object.sectionItems) {
			Country *country = (Country *)[object.sectionItems objectAtIndex:indexPath.row];
			cell.textLabel.text = country.countryName;
			//cell.detailTextLabel.text = [NSString stringWithFormat:@"(+%@)", country.dialingCode];
		}
	}
	
	NSString *myCountry = [app.uiManager.settings getMyLocation];
	
	NSLog(@"%@", myCountry);
	
	if ([myCountry isEqualToString:cell.textLabel.text])
	{
		cell.textLabel.textColor = [UIColor blueColor];
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
		lastIndexPath = [indexPath retain];
	}
	else
	{
		cell.textLabel.textColor = [UIColor blackColor];
		cell.accessoryType = UITableViewCellAccessoryNone;
	}
	
    return cell;
}


@end
